﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using CWCS.UIDesigner.DTSControl;

namespace CWCS.UI
{
    public partial class FrmDTSManager : Form
    {
        Thread threadDTS;
        private delegate void RichBoxCallBack(RichTextBox rtb, string msg);
        private delegate void LabelCallBack(string state,DTSControl dTSControl);         
        private string DTSIP1 = string.Empty;
        private string DTSIP2 = string.Empty;
        private string zonecode = string.Empty;
        public Dictionary<string, string> listDts = new Dictionary<string, string>();
        public string ip = string.Empty;
        public int port;
        // private int DTSPort = 0;

        public FrmDTSManager()
        {
            InitializeComponent();
        }

        private void FrmDTSManager_Load(object sender, EventArgs e)
        {
            txt_ConnectState.BackColor = Color.ForestGreen;
            txt_ConnectState2.BackColor = Color.ForestGreen;

            zonecode = System.Configuration.ConfigurationManager.AppSettings["ZoneCode"].ToString().Trim();
            //DTSIP = System.Configuration.ConfigurationManager.AppSettings["DTSIP"].ToString().Trim();
            //DTSPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["DTSPort"].ToString().Trim());

            if (zonecode=="HC")
            {
                DTSIP1 = System.Configuration.ConfigurationManager.AppSettings["DTSIP1"].ToString().Trim();
                this.label2.Visible = false;
                this.txt_ConnectState2.Visible = false;
                listDts.Add(DTSIP1, "8989");
            }
           else if(zonecode =="FR")
            {
                DTSIP1 = System.Configuration.ConfigurationManager.AppSettings["DTSIP2"].ToString().Trim();
                DTSIP2 = System.Configuration.ConfigurationManager.AppSettings["DTSIP3"].ToString().Trim();
                listDts.Add(DTSIP1, "8989");
                //listDts.Add(DTSIP2, "8989");
            }
            threadDTS = new Thread(new ThreadStart(DoWork));
            threadDTS.Name = "frmDTS";
            threadDTS.IsBackground = true;
            threadDTS.Start();

        }
        private void DoWork()
        {
            foreach(var i in listDts)
            {
                ip = i.Key;
                port = int.Parse(i.Value);
                BLL.BLLDTS bll = new BLL.BLLDTS();
                bll.ShowDTSMsg += new BLL.BLLDTS.DTSNotifyMessage(RecevingMessage);
                bll.InitServer(ip, port);

            }             
        }
        private void ShowText(RichTextBox rtb, string msg)
        {
            try
            {
                if (rtb.InvokeRequired)
                {
                    RichBoxCallBack rb = new RichBoxCallBack(ShowText);
                    this.Invoke(rb, new object[] { rtb, msg });
                }
                else
                {
                    if (!string.IsNullOrEmpty(msg))
                    {
                        if (rtb.TextLength > 5000)
                            rtb.Clear();
                        if (!System.String.IsNullOrEmpty(msg))
                            rtb.AppendText(System.DateTime.Now.ToString() + ": " + msg + "\n");
                    }
                }
            }
            catch (Exception E)
            { }
        }
        private void ShowConnectState(string state, DTSControl dTSControl)
        {
            if(dTSControl.m_serverIP== DTSIP1)
            {
                if (txt_ConnectState.InvokeRequired)
                {
                    LabelCallBack lcb = new LabelCallBack(ShowConnectState);
                    this.Invoke(lcb, new object[] { state, dTSControl });
                    //  txt_ConnectState.BackColor = Color.Lime;
                }
                else
                {
                    txt_ConnectState.Text = state;
                    if (state == "已连接")
                    {
                        txt_ConnectState.BackColor = Color.Lime;
                        txt_ConnectState.Text = dTSControl.m_serverIP + state;
                    }
                    else if (state == "已断开")
                    {
                        txt_ConnectState.BackColor = Color.Red;
                        txt_ConnectState.Text = dTSControl.m_serverIP + state;
                    }
                    else if (state == "正在连接")
                    {
                        txt_ConnectState.BackColor = Color.Lime;
                        txt_ConnectState.Text = dTSControl.m_serverIP + state;
                    }
                }
            }
         else if(dTSControl.m_serverIP == DTSIP2)
            {
                if (txt_ConnectState2.InvokeRequired)
                {
                    LabelCallBack lcb = new LabelCallBack(ShowConnectState);
                    this.Invoke(lcb, new object[] { state, dTSControl });
                    //  txt_ConnectState1.BackColor = Color.Lime;
                }
                else
                {
                    txt_ConnectState2.Text = state;
                    if (state == "已连接")
                    {
                        txt_ConnectState2.BackColor = Color.Lime;
                        txt_ConnectState2.Text = dTSControl.m_serverIP + state;
                    }
                    else if (state == "已断开")
                    {
                        txt_ConnectState2.BackColor = Color.Red;
                        txt_ConnectState2.Text = dTSControl.m_serverIP + state;
                    }
                    else if (state == "正在连接")
                    {
                        txt_ConnectState2.BackColor = Color.Lime;
                        txt_ConnectState2.Text = dTSControl.m_serverIP + state;
                    }
                }
            }
        }

        private void RecevingMessage(string Command, DTSControl dTSControl, object Msg)
        {
            if (Command == "Linking")
            {
                ShowConnectState("正在连接", dTSControl);
            }
            else if (Command == "Connect")
            {
                ShowText(richTextBox2, Msg.ToString());

                ShowConnectState("已连接", dTSControl);
            }
            else if (Command == "Break")
            {
                ShowText(richTextBox2, Msg.ToString());
                ShowConnectState("已断开", dTSControl);
            }
            else
            {
                ShowText(richTextBox1, Msg.ToString());
            }

        }
    }
}
